<?php

namespace Estore\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

use Estore\Model\Storeproducts;
use Estore\Model\Storeorders;
use Estore\Form\StoreproductsForm;

/**
 * StoreAdmin
 *
 * @author
 *
 * @version
 *
 */
class StoreAdminController extends AbstractActionController {
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
			// grab the paginator from the Store Product
    	$ProductsTable = $this->getServiceLocator()->get('ProductsTable');
    	//$paginator = $this->getAlbumTable()->fetchAll(true);
    	$paginator = $ProductsTable->fetchAll(true);
    	// set the current page to what has been passed in query string, or to 1 if none set
    	$paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
    	// set the number of items per page to 10
    	$paginator->setItemCountPerPage(8);
    	//throw new \Exception(var_dump($paginator));
    	return new ViewModel(array(	'paginator' => $paginator));
    	//return new ViewModel();
   		//return array();
	}
	
	public function addAction()
	{
		$form = new StoreproductsForm();
	
		$request = $this->getRequest();
	
		if ($request->isPost()) {
			$Storeproducts = new Storeproducts();
			//$form->setInputFilter($Storeproducts->getInputFilter());
			$form->setData($request->getPost());
			 
			if (!$form->isValid())
			{
				$viewModel = new ViewModel(array(
						'error' => true,
						'form' => $form,
				));
				$viewModel->setTemplate('estore/add');
				return $viewModel;
			}
			else
			{
				$Storeproducts->exchangeArray($form->getData());
				$ProductsTable = $this->getServiceLocator()->get('ProductsTable');
				$ProductsTable->saveStoreproducts($Storeproducts);
	
				// Redirect to list of products
				return $this->redirect()->toRoute('estore');
			}
		}
		$viewModel = new ViewModel(array('form' => $form));
		return $viewModel;
	}
	
	public function deleteAction() {
		 
		$id = (int) $this->params()->fromRoute('id', 0);
		if (!$id) {
			return $this->redirect()->toRoute('storeadmin');
		}
	
		$request = $this->getRequest();
		if ($request->isPost()) {
			$del = $request->getPost('del', 'No');
	
			if ($del == 'Yes') {
				$id = (int) $request->getPost('id');
				//$this->getUserTable()->deleteAlbum($id);
				$productTable = $this->getServiceLocator()->get('ProductsTable');
				$productTable->deleteStoreproducts($id);
			}
			// Redirect to list of users
			return $this->redirect()->toRoute('storeadmin');
		}
		//we are getting product w id from params
		$productTable = $this->getServiceLocator()->get('ProductsTable');
		$product = $productTable->getStoreproducts($id);
		 
		$viewModel = new ViewModel(array('product' => $product,'id'=>$id,));
		return $viewModel;
		 
	}
	
	public function editproductAction()
	{
		$id = (int) $this->params()->fromRoute('id', 0);
		if (!$id) {
			return $this->redirect()->toRoute('storeadmin', array(
					'action' => 'add'
			));
		}
	
		// Get the Product with the specified id.  An exception is thrown
		// if it cannot be found, in which case go to the index page.
		$productTable = $this->getServiceLocator()->get('ProductsTable');
		$product = $productTable->getStoreproducts($id);
	
		$form = new StoreproductsForm();
		$form->bind($product);
		$form->get('submit')->setAttribute('value', 'Edit');
	
		$request = $this->getRequest();
		if ($request->isPost()) {
			//$form->setInputFilter($product->getInputFilter());
			$form->setData($request->getPost());
	
			if ($form->isValid()) {
				$productTable->saveStoreproducts($product);
	
				// Redirect to list of albums
				return $this->redirect()->toRoute('storeadmin');
			}
		}
	
		return array(
				'id' => $id,
				'form' => $form,
		);
	}
	
	public function productDetailAction()
	{
		$request = $this->getRequest();
		 
		if ($request->isPost())
		{
			$choose = $request->getPost('choose', 'Purchase');
			//throw new \Zend\File\Transfer\Exception\InvalidArgumentException(var_dump($del));
			if ($choose == 'Purchase') {
				$productId = (int) $request->getPost('id');
				$qnt = 	(int) $request->getPost('qnt');
				// Redirect to list shoping Cart
				return $this->redirect()->toRoute('estore',array('action' => 'shoppingCart','id'=>$productId,'qnt'=>$qnt),array('qnt'=>$qnt,));
				//return $this->url('estore',array('action' => 'shoppingCart','id'=>$id,),array('qnt'=>$qnt));
			}
			// Redirect to list shoping Cart
			return $this->redirect()->toRoute('storeadmin',array('action' => 'index'));
			//return $this->url('estore',array('action' => 'shoppingCart','id'=>$id,),array('qnt'=>$qnt));
			 
		}
		else
		{
			$productId = $this->params()->fromRoute('id');
			$productTable = $this->getServiceLocator()->get('ProductsTable');
			$product = $productTable->getStoreproducts($productId);
			$viewModel = new ViewModel(array('product' => $product));
		}
		 
		//return new ViewModel();
		return $viewModel;
	}
	
	public function storeordersAction()
	{
		// grab the paginator from the Store Product
		$OrdersTable = $this->getServiceLocator()->get('OrdersTable');
		$paginator = $OrdersTable->fetchAll(true);
		// set the current page to what has been passed in query string, or to 1 if none set
		$paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
		// set the number of items per page to 10
		$paginator->setItemCountPerPage(8);
		//throw new \Exception(var_dump($paginator));
		//return new viewModel();
		return new ViewModel(array(	'paginator' => $paginator));
	}
	
	public function vieworderAction()
	{	
		$request = $this->getRequest();
		if ($request->isPost()) {
			$status = $request->getPost('status', 'New');
		    $id = (int) $request->getPost('id');
		    $ordersTable = $this->getServiceLocator()->get('OrdersTable');
		    $order = $ordersTable->getStoreorders($id);
		    /////////////////////////////////////////////////
		    $exchange_data = array();
		    $exchange_data['id'] = $id;
		    $exchange_data['store_product_id'] = $order->store_product_id;
		    $exchange_data['qty'] = $order->qty;
		    $exchange_data['total'] = $order->total;
		    $exchange_data['status'] = $order->status;
		    $exchange_data['stamp'] = $order->stamp;
		    $exchange_data['first_name'] = $order->first_name;
		    $exchange_data['last_name'] = $order->last_name;
		    $exchange_data['email'] = $order->email;
		    $exchange_data['ship_to_street'] = $order->ship_to_street;
		    $exchange_data['ship_to_city'] = $order->ship_to_city;
		    $exchange_data['ship_to_state'] = $order->ship_to_state;
		    $exchange_data['ship_to_state'] = $order->ship_to_state;
		    $exchange_data['ship_to_zip'] = $order->ship_to_zip;
		    //////////////////////////////////////////////////////
			if ($status == 'New') {
				$exchange_data['status'] = 'New';
			}
			elseif  ($status == 'Completed') {
				$exchange_data['status'] = 'Completed';
			}
			elseif ($status == 'Shipped') {
				$exchange_data['status'] = 'Shipped';
			}
			elseif ($status == 'Cancelled') {
				$exchange_data['status'] = 'Cancelled';
			}
			///////////////////////////////////////////////////////////
			$order = new Storeorders();
			$order->exchangeArray($exchange_data);
			$ordersTable->saveStoreorders($order);
			// Redirect to list of users
			return $this->redirect()->toRoute('storeadmin',array('action'=>storeorders));
		}
		
		$orderId = $this->params()->fromRoute('id');
		$orderTable = $this->getServiceLocator()->get('OrdersTable');
		$order = $orderTable->getStoreorders($orderId);
		$productId = $order->store_product_id;
		//****************************************
		$productTable = $this->getServiceLocator()->get('ProductsTable');
		$product = $productTable->getStoreproducts($productId);
		
		$viewModel = new ViewModel(array('order' => $order, 'product' => $product,));
		
		return $viewModel;
	}
	
	/////////////////////////////////////////////////////////////////////
	public function SetExpressCheckoutAction()
	{
		return new ViewModel();
	}
	
	
}